﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using Caliburn.Micro;
using Google.GData.YouTube;

namespace AppReviewerClient
{
    public class YouTubeLoginViewModel : ViewAware
    {

        private string _tempUsername;
        private string _tempPassword;
        private bool _tempRememberMe;

        private string _rememberMe;
        public string RememberMe
        {
            get { return _rememberMe; }
            set
            {
                _rememberMe = value;
                NotifyOfPropertyChange(() => RememberMe);
            }
        }

        private string _username;
        public string Username
        {
            get { return _username; }
            set
            {
                _username = value;
                // only autoupdate this if it has not changed yet
                if (_accountChanged == false)
                {
                    if (value != null)
                    {
                        _account = value.Split('@')[0];

                        NotifyOfPropertyChange(() => Account);
                    }
                }

                NotifyOfPropertyChange(() => Username);
            }
        }
        private string _account;
        public string Account
        {
            get { return _account; }
            set
            {
                _account = value;
                _accountChanged = true;

                NotifyOfPropertyChange(() => Account);
            }
        }
        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                NotifyOfPropertyChange(() => Password);
            }
        }

        private readonly ShellViewModel _parent;
        private bool _accountChanged = false;

        public YouTubeLoginViewModel(ShellViewModel parent)
        {
            _parent = parent;
            //var array = PathFolderHelper.GetCredentials();

            //_tempUsername = Username = array["username"];
            //_tempPassword = Password = array["password"];

        }

        private bool? _isCredentialsChecked;

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);

            LoginCmd();
        }

        private bool _isWaiting;
        public bool IsWaiting
        {
            get { return _isWaiting; }
            set
            {
                _isWaiting = value;
                NotifyOfPropertyChange(() => IsWaiting);
            }
        }


        /// <summary>
        /// Prova credenziali
        /// </summary>
        public void LoginCmd()
        {
            if (!_isCredentialsChecked.HasValue || !_isCredentialsChecked.Value)
                if (!(string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password) || string.IsNullOrWhiteSpace(Account)))
                {
                    var service = new YouTubeService("AppReviewer");
                    service.setUserCredentials(Username, Password);

                    //textbox1 contains username and maskedTextBox1 contains password
                    try
                    {
                        var success = service.QueryClientLoginToken();
                        _parent.YouTubeUploader.Login(Username, Password, Account);
                        CloseWindow();
                    }
                    catch (System.Net.WebException e)
                    {
                        MessageBox.Show("Wrong Credentials");
                    }
                }
        }

        public void CancelCmd()
        {
            // chiude finestra
            Username = _tempUsername;
            Password = _tempPassword;

            CloseWindow();
        }

        public void CloseWindow()
        {
            (GetView() as Window).Close();

        }

        public void LogOffCmd()
        {
            Username = string.Empty;
            Password = string.Empty;

            _isCredentialsChecked = false;

            //SaveCredentials();
        }

        //private void SaveCredentials()
        //{
        //    _tempUsername = Username;
        //    _tempPassword = Password;

        //    PathFolderHelper.SaveCredentials(Username, Password,false);

        //    _parent.DisplayUsername = Username;
        //}

    }
}
